package com.geen.tuoke.ui.dialog

import android.content.Context
import android.graphics.Color
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.TimeUtils
import com.geen.tuoke.R
import com.geen.tuoke.bean.Label
import com.geen.tuoke.bean.request.RecreateTaskRequest
import com.geen.tuoke.databinding.DialogTaskPhoneCreateCallTaskBinding
import com.geen.tuoke.ui.adapter.LabelAdapter
import com.geen.tuoke.utils.ext.dp2px
import com.geen.tuoke.widget.dialog.DialogViewHolder
import com.geen.tuoke.widget.dialog.XXDialog
import com.qmuiteam.qmui.kotlin.onClick
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import java.util.*

/**
 * @Desc 新建重新拨号任务弹窗
 */
class DialogTaskPhoneCreateCallTask(
    context: Context,
    private var onConfirm: (task: RecreateTaskRequest) -> Unit
) : XXDialog(context, R.layout.dialog_task_phone_create_call_task) {

    lateinit var adapter: LabelAdapter
    private var mBinding: DialogTaskPhoneCreateCallTaskBinding? = null


    init {
        setWidthAndHeight(dp2px(288f), 0)
        setCanceledOnTouchOutside(false)
        setCancelAble(false)


        mBinding?.run {
            //-1未拨打0未接通1接通2空号
            val llList = listOf<LinearLayout>(ll1, ll2, ll3, ll4)
            llList.forEach { contains ->
                contains.isSelected = false
                contains.onClick {
                    contains.isSelected = !contains.isSelected
                }
            }


            initRV()
            tvCancel.onClick {
                dismiss()
            }
            tvConfirm.onClick {
                val taskText = etTaskName.text.toString()
                val selectTagList = llList.filter { it.isSelected }.map { it.tag as String }
                val labels = adapter.data.filter { it.isSelect }

                var task = RecreateTaskRequest(0, taskText, labels, selectTagList)
                onConfirm(task)
                dismiss()
            }

        }

    }


    override fun convert(holder: DialogViewHolder) {
        mBinding = DataBindingUtil.bind(holder.convertView)

    }

    fun setLabelData(it: List<Label>?) {
        adapter.setList(it)
    }


    private fun DialogTaskPhoneCreateCallTaskBinding.initRV() {
        adapter = LabelAdapter()
        rv.layoutManager = GridLayoutManager(context, 3)
        rv.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(
                context
            ).size(dp2px(15f))
                .color(Color.TRANSPARENT).build()
        )
        rv.adapter = adapter
    }


    var count = 1

    fun showAutoDialog() {
        mBinding?.run {
            val taskText = TimeUtils.date2String(Date(), "yyyyMMdd$count")
            etTaskName.setText(taskText)
            count++
        }
        showDialog()

    }


}